﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace WindowsFormsTest
{
    public class DBlogFormatter : DatabaseLogFormatter
    {
        public DBlogFormatter(DbContext context, Action<string> writeAction) : base(context, writeAction)
        {
        }

        public override void LogCommand<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
        {
            if (command.Connection is OracleConnection)
            {
                command.CommandText = command.CommandText.Replace("@", ":");
            }
            string sql = command.CommandText;

            foreach (var parm in command.Parameters)
            {
                //parm是参数名字段
                string outPutParamValue = command.Parameters[$"{parm}"].Value.ToString();//参数值
                if (command.Connection is System.Data.SqlClient.SqlConnection)
                {
                    sql = sql.Replace($"@{parm}", $"'{outPutParamValue}'");
                }
                else if (command.Connection is OracleConnection)
                {
                    sql = sql.Replace($":{parm}", $"'{outPutParamValue}'");
                }
               
                //获取对应的参数的值：
                //Write($"参数名=参数值是：{parm}={outPutParamValue}");//输出看看。
            }

            //Write($"重写LogCommand:记录将要执行的命令:{command.CommandText}");
            Write($"输出的SQL语句：\r\n{sql}{Environment.NewLine}");
            
            //Write($"数据库连接字符串:{command.Connection.ConnectionString}{Environment.NewLine}");

            //Write(/*$"重写LogCommand:记录将要执行的命令:command.CommandText:{command.CommandText}{Environment.NewLine}\r\n" +*/
            //     $"输出的SQL语句：{sql}{Environment.NewLine}\r\n" +
            //     $"数据库执行时间:{command.CommandTimeout}{Environment.NewLine}" +
            //     $"数据库连接字符串:{command.Connection.ConnectionString}{Environment.NewLine}");
            //$"数据执行类型(存储过程或者表名称或者SQL 文本命令):{command.CommandType}{Environment.NewLine}" +
            //$"command.Container:{command.Container}{Environment.NewLine}" +
            //$"command.Parameters:{command.Parameters}{Environment.NewLine}" +
            //$"command.Site:{command.Site}{Environment.NewLine}" +
            //$"command.ToString():{command.ToString()}{Environment.NewLine}" +

            if (command.CommandTimeout>60) //超过一分钟才打印执行时间
            {
                Write($"数据库执行时间:{command.CommandTimeout}{Environment.NewLine}");
            }

            if (command.Transaction!=null)
            {
                Write($"command.Transaction事务:{command.Transaction}{Environment.NewLine}");
            }
            if (command.UpdatedRowSource!= UpdateRowSource.Both)
            {
                Write($"command.UpdateRowSource:{command.UpdatedRowSource}{Environment.NewLine}");//UpdateRowSource是DbCommand的一个属性，当DbCommand把本地变化更新到数据库的时候，UpdateRowSource可能把数据库的变化带回本地。不支持返回结果集和输出参数的 UpdatedRowSource.Both。
            }
        }
        //public override void Closing(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
        //{
        //    Write($"重写Closing:{Environment.NewLine}" +
        //        $"connection.ConnectinoString:{connection.ConnectionString}{Environment.NewLine}" +
        //        $"connection.ConnectionTimeout:{connection.ConnectionTimeout}{Environment.NewLine}" +
        //        $"connection.Container:{connection.Container}{Environment.NewLine}" +
        //        $"connection.Database:{connection.Database}{Environment.NewLine}" +
        //        $"connection.DataSource:{connection.DataSource}{Environment.NewLine}" +
        //        $"connection.ServerVersion:{connection.ServerVersion}{Environment.NewLine}" +
        //        $"connection.Site:{connection.Site}{Environment.NewLine}");
        //    base.Closing(connection, interceptionContext);
        //}
    }
}
